package com.xwh.shop.goods.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xwh.shop.goods.model.entity.GoodsImage;
import com.xwh.shop.goods.service.mapper.GoodsImageMapper;
import com.xwh.shop.goods.service.service.GoodsImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwh
 * @since 2025-07-16
 */
@Service
public class GoodsImageServiceImpl extends ServiceImpl<GoodsImageMapper, GoodsImage> implements GoodsImageService {

    @Override
    public String getMainImageByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsImage::getGoodsId, goodsId);
        wrapper.eq(GoodsImage::getIsMain, 1);

        GoodsImage image = this.getOne(wrapper);
        return image.getImageUrl();
    }

    @Override
    public List<String> getImagesByGoodsId(Long goodsId) {
        List<String> images = new ArrayList<>();
        LambdaQueryWrapper<GoodsImage> wrapperMain = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<GoodsImage> wrapperSlave = new LambdaQueryWrapper<>();

        wrapperMain.eq(GoodsImage::getGoodsId, goodsId);
        wrapperSlave.eq(GoodsImage::getGoodsId, goodsId);
        wrapperMain.eq(GoodsImage::getIsMain, 1);
        wrapperSlave.eq(GoodsImage::getIsMain, 0);
        //      主图
        GoodsImage mainImage = this.getOne(wrapperMain);
        images.add(mainImage.getImageUrl());
        //      非主图
        // 不能这样写 否则查询语句会变成 select * from goods_image where goods_id = ? and is_main = ? and is_main = ?
        // wrapperMain.eq(GoodsImage::getIsMain, 0);

        //非主图有多个，所以用list
        List<GoodsImage> slaveImages = this.list(wrapperSlave);
        for (GoodsImage slaveImage : slaveImages) {
            images.add(slaveImage.getImageUrl());
        }
        return images;
    }
}
